$(function () {
    //1.抽取变量
    var role_datagrid = $("#role_datagrid");
    var role_dialog = $("#role_dialog");
    var role_form = $("#role_form");

    role_datagrid.datagrid({
        url: '/role/query.do',
        fit: true,
        fitColumns: true,
        toolbar: '#role_toolbar',
        pagination: true,
        rownumbers: true,
        striped: true,
        singleSelect: true,
        columns: [
            [
                {field: 'sn', title: '角色编号', width: 80},
                {field: 'name', title: '角色名称', width: 80}
            ]]

    });

    var menu_combotree = $("#menu_combotree");

    //菜单树形下拉框
    menu_combotree.combotree({
        //选择复选框
        onCheck:function (node,checked) {
            //获取tree树形对象
            var treeObj = menu_combotree.combotree('tree');
                //获取该节点的父菜单
            var parent = treeObj.tree('getParent', node.target);
                if (parent) {
                    //把父菜单设置为勾选状态
                    treeObj.tree('check', parent.target);
                }
        }
    })


   /* var currentNode = null;
    //菜单树形下拉框
    menu_combotree.combotree({
        onSelect:function(node){
            currentNode = node;
        },
        //选择复选框
        onCheck:function (node,checked) {
            //获取tree树形对象
            var treeObj = menu_combotree.combotree('tree');
            if(currentNode==node){
                //获取该节点的父菜单
                var parent = treeObj.tree('getParent', node.target);
                if (parent) {
                    //把父菜单设置为勾选状态
                    treeObj.tree('check', parent.target);
                }else {
                    //把子菜单设置为勾选状态
                    var children = treeObj.tree('getChildren', node.target);
                    console.log(children);
                    for (var i = 0; i < children.length; i++) {
                        if (checked) {
                            treeObj.tree('check', children[i].target);
                        } else {
                            treeObj.tree('uncheck', children[i].target);
                        }
                    }
                }
            }
        }
    })*/

    //2.使用一个对象来统一管理事件的方法
    var methodObj = {

        add: function () {
            //打开弹出框
            role_dialog.dialog('open');
            role_dialog.dialog('setTitle', '新增角色');
        },

        edit: function () {
            var row = role_datagrid.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'warning');
                return;
            }

            //回显数据
            role_form.form('load', row);

            //回显权限数据
            selfPermissions.datagrid('load',{roleId:row.id});

            $.get('/menu/selectByRoleId.do',{roleId:row.id},function (data) {
                //回显菜单数据
                menu_combotree.combotree('setValues',data);
            });

            //打开弹出框
            role_dialog.dialog('open');
            role_dialog.dialog('setTitle', '新增角色');
        },

        save: function () {
            //提交表单
            role_form.form('submit', {
                url: '/role/saveOrUpdate.do',
                onSubmit:function(param){
                    //获取已有权限中的所有行
                    var rows = selfPermissions.datagrid('getRows');
                    for(var i=0;i<rows.length;i++){
                        //添加权限的参数
                        param["permissions["+i+"].id"] = rows[i].id;
                    }

                    //获取菜单树形下拉框选中的值
                    var menus = menu_combotree.combotree('getValues');
                    for(var i=0;i<menus.length;i++){
                        //添加菜单的参数
                        param["menus["+i+"].id"] = menus[i];
                    }

                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        //提示用户操作结果
                        $.messager.alert('温馨提示', '保存成功!', 'info', function () {
                            methodObj.cancel();
                            role_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', '保存失败!', 'error');
                    }
                }
            })


        },

        cancel: function () {
            role_dialog.dialog('close');
        }

    }

    //3.统一绑定事件
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })


    var allPermissions = $("#allPermissions");
    var selfPermissions = $("#selfPermissions");

    role_dialog.dialog({
        width: 550,
        height: 450,
        buttons: '#form_btns',
        // closed: true,
        onClose: function () {
            //清空表单
            role_form.form('clear');
            //清空已有权限
            selfPermissions.datagrid('loadData',[]);
            //重新把所有权限查询出来
            allPermissions.datagrid('reload');
        }
    })

    selfPermissions.datagrid({
        title:'已有权限',
        height:300,
        fitColumns:true,
        url:'/permission/selectByRoleId.do',
        columns:[[
            {field: 'name', title: '权限名称', width: 80,align:'center'}
        ]],
        onClickRow:function (index,row) {
            //往所有权限中添加点击的数据
            allPermissions.datagrid('appendRow',row);
            //把点击的数据从已有权限中移除掉
            selfPermissions.datagrid('deleteRow',index);
        },
        onLoadSuccess:function (data) {
            //已有权限
            console.log(data.rows);
            //处理所有权限的数据,如果已经出现在已有权限,那么就需要从所有权限中删除掉
            //1.把已有权限的权限id放到一个ids数组
            var ids = $.map(data.rows,function (p) {
                return p.id;
            })
            //2.遍历所有权限的数据
            var rows = allPermissions.datagrid('getRows');
            for(var i=rows.length-1;i>=0;i--){
                var permission = rows[i];
                //3.如果所有权限中的权限id是存在ids中,意味着角色已经拥有该权限,就需要移除掉
                var index = $.inArray(permission.id,ids);
                if(index>=0){
                    allPermissions.datagrid('deleteRow',i);
                }
            }
        }
    });

    allPermissions.datagrid({
        title:'所有权限',
        height:300,
        url:'/permission/selectAll.do',
        fitColumns:true,
        columns:[[
            {field: 'name', title: '权限名称', width: 80,align:'center'}
        ]],
        onClickRow:function (index,row) {
            //往已有权限中添加点击的数据
            selfPermissions.datagrid('appendRow',row);
            //把点击的数据从所有权限中移除掉
            allPermissions.datagrid('deleteRow',index);
        }
    });




})


